export class Sort {
  ascending: boolean
  descending: boolean
  direction: string
  ignoreCase: boolean
  nullHandling: string
  property: string
}

export class Page<T> {
  content: Array<T> | null
  first: boolean
  last: boolean
  number: number
  numberOfElement: number
  size: number
  sort: Sort | null
  totalElements: number
  totalPages: number
}

export class PageBuilder {
  pageNum = 1 // 页码
  pageSize = 10 // 页面大小
  sort = 'id' // 默认id排序
  dir = 'DESC' // 默认降序

  buildPageNum(num: number): PageBuilder {
    this.pageNum = num
    return this
  }
  buildPageSize(num: number): PageBuilder {
    this.pageSize = num
    return this
  }
  buildSort(sort: string): PageBuilder {
    this.sort = sort
    return this
  }
  buildDir(dir: string): PageBuilder {
    this.dir = dir
    return this
  }
  concatData(data: any): any {
    return Object.assign({ pageBuilder: this }, data)
  }
}
export function buildPageBuilder(): PageBuilder { return new PageBuilder() }
export function jsonToPage<T>(json: any): Page<T> {
  const page = new Page<T>()
  page.first = json['first']
  page.last = json['last']
  page.number = json['number']
  page.size = json['size']
  page.sort = json['sort']
  page.totalElements = json['totalElements']
  page.totalPages = json['totalPages']

  page.content = Array.from(<ArrayLike<T>>json['content'])
  return page
}
